package airthmetic.exercise.heap;

import java.util.PriorityQueue;

public class _1046_最后一块石头的重量 {
    public static int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>((o1, o2)->{
            return o2-o1;
        });

        for(int stone : stones){
            queue.offer(stone);
        }

        while(!queue.isEmpty()){
            Integer i_1 = queue.poll();
            if(queue.isEmpty()){
                return i_1;
            }else{
                Integer i_2 = queue.poll();
                if(i_1.intValue() == i_2.intValue()){
                    continue;
                }else{
                    queue.offer(i_1 > i_2 ? i_1-i_2 : i_2-i_1);
                }
            }
        }

        return 0;
    }

    public static void main(String[] args) {
        System.out.println(lastStoneWeight(new int[]{1, 2, 2, 3, 4, 4, 4}));
    }
}
